package top.huxy.boot.config.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import top.huxy.boot.util.obj.ThreadLocalRequestDetail;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Classname RecordRequestInterceptor
 * @Description 请求记录信息
 * @Date 2021/9/16 21:43
 * @Created by huxy
 */
@Slf4j
@Deprecated
public class RecordRequestInterceptor implements HandlerInterceptor {

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //控制器执行完成，设置完成标识
        ThreadLocalRequestDetail requestDetail = ThreadLocalRequestDetail.current();
        if (requestDetail != null) {
            requestDetail.setOk(true);
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //请求完成，记录日志
        ThreadLocalRequestDetail requestDetail = ThreadLocalRequestDetail.current();
        if (requestDetail != null) {
            //记录访问日志
            requestDetail.logVisitor(requestDetail.isOk());
            ThreadLocalRequestDetail.destroy();
        }
    }
}
